import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
import random
import os


def scrape_douban_movies():
    url = "https://movie.douban.com/top250"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0 Safari/537.36",
    }

    movie_list = []

    # 每页有25部电影，10页共250部
    for page in range(10):
        print(f"正在爬取第 {page + 1} 页...")

        # 控制分页
        params = {"start": page * 25}
        response = requests.get(url, headers=headers, params=params)

        if response.status_code != 200:
            print(f"请求失败，状态码：{response.status_code}")
            break

        soup = BeautifulSoup(response.text, "html.parser")
        movies = soup.select(".item")

        for movie in movies:
            try:
                # 获取电影名称
                name = movie.select_one(".title").text

                # 获取电影评分
                rating = movie.select_one(".rating_num").text if movie.select_one(".rating_num") else "未知"

                # 获取导演和主演等信息
                bd_info = movie.select_one(".bd p").text.strip()
                details = bd_info.split("\n")

                # 导演：通常是第一行
                dire_actor = details[0].strip()

                others = details[1].strip() if len(details) > 1 else "未知"

                # 获取影评：通常在span标签 class='inq' 内
                best_review = movie.select_one(".inq")
                best_review_text = best_review.text if best_review else "暂无"

                # 将数据添加到列表中
                movie_list.append({
                    "电影名称": name,
                    "评分": rating,
                    "导演/主演": dire_actor,
                    "上映年份/国别/类型": others,
                    "一句话影评": best_review_text,
                })

            except Exception as e:
                print(f"解析出错: {e}")
                continue

        # 随机延迟，防止被反爬
        time.sleep(random.uniform(1, 3))

    # 保存数据到CSV
    output_dir = "./data"
    output_path = os.path.join(output_dir, "raw_data.